package com.atguigu02.trycatcgfinally;

import org.junit.Test;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * ClassName: ExceptionHandleTest
 * Package: com.atguigu02.trycatcgfinally
 * Description:
 *
 * @Author ZY
 * @Create 20:20
 * @Version 1.0
 */
public class ExceptionHandleTest {

    @Test
    public void test1() {
        try {
            Scanner scanner = new Scanner(System.in);
            int num = scanner.nextInt();
            System.out.println(num);

        } catch (InputMismatchException e) {
            System.out.println("出现了InputMismatchException的异常");
        } catch (NullPointerException e) {
            System.out.println("出现了NullPointerException的异常");
        } catch (RuntimeException e) {
            System.out.println("出现了RuntimeException的异常");
        }

        System.out.println("异常处理结束，代码继续执行...");
    }

    @Test
    public void test2(){
        try {
            String str = "123";
            str = "abc";
            int i = Integer.parseInt(str);
            System.out.println(i);
        }catch (NumberFormatException e){
//            e.printStackTrace();
            System.out.println(e.getMessage());

            System.out.println(10 / 0);
        }finally{
            System.out.println("无论是否出现异常，都会执行finally中的代码");
        }

        System.out.println("程序结束");
    }




}
